package com.service.abolishing.controller;

import com.service.abolishing.dto.BerthInfoDto;
import com.service.abolishing.entity.BerthInfo;
import com.service.abolishing.service.BerthInfoService;
import com.service.config.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.service.config.constant.Constant.*;

/**
 * 泊位信息表
 * (BerthInfo)表控制层
 *
 * @author makejava
 * @since 2021-10-21 14:14:30
 */
@RestController
@RequestMapping("berthInfo")
@Api(value = "泊位信息接口", tags = "泊位信息接口")
public class BerthInfoController {
    /**
     * 服务对象
     */
    @Resource
    private BerthInfoService berthInfoService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 新增泊位信息
     * @param cloudAuthToken
     * @return
     * @throws Exception
     */
    @PostMapping(value = "getBerthInfo",produces="application/json")
    @ApiOperation(value = "新增泊位信息")
    @ResponseBody
    public Result addBerthInfo(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                               @RequestBody BerthInfoDto berthInfoDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                berthInfoDto.setManageInfoId(userId);
                BerthInfo berthInfo = ModelMapperUtil.strictMap(berthInfoDto, BerthInfo.class);
                berthInfo.setId(IdUtil.getStringId());
                berthInfoService.insert(berthInfo);
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    /**
     * 获取泊位信息
     * @param cloudAuthToken
     * @return
     * @throws Exception
     */
    @GetMapping(value = "getBerthInfo",produces="application/json")
    @ApiOperation(value = "获取泊位信息")
    @ResponseBody
    public Result getBerthInfo(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                BerthInfo berthInfo = new BerthInfo();
                berthInfo.setManageInfoId(userId);
                List<BerthInfo> berthInfoList = berthInfoService.queryAll(berthInfo);
                return Result.ok().setData(berthInfoList);
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    /**
     * 获取泊位信息
     * @param cloudAuthToken
     * @return
     * @throws Exception
     */
    @PostMapping(value = "updateBerthInfo",produces="application/json")
    @ApiOperation(value = "修改泊位信息")
    @ResponseBody
    public Result updateBerthInfo(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                  @RequestBody BerthInfoDto berthInfoDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                berthInfoDto.setManageInfoId(userId);
                berthInfoService.update(ModelMapperUtil.strictMap(berthInfoDto,BerthInfo.class));
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    /**
     * 删除泊位信息
     * @param cloudAuthToken
     * @return
     * @throws Exception
     */
    @GetMapping(value = "deleteBerthInfo",produces="application/json")
    @ApiOperation(value = "删除泊位信息")
    @ResponseBody
    public Result deleteBerthInfo(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                  @RequestParam String bId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                berthInfoService.deleteById(bId);
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
}
